package com.kcs.fxmargin.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.kcs.common.utils.Constant;
import com.kcs.fxmargin.action.form.DepositWithdrawActionForm;
import com.kcs.fxmargin.bo.DepositWithdrawBO;
import com.kcs.fxmargin.service.AccOnlineService;

public class DepositWithdrawAction extends FXAction {
	private Logger log = Logger.getLogger(Constant.LOG_FX);
	
	AccOnlineService service = new AccOnlineService();

	@Override
	public ActionForward process(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{ 
		try{

			log.debug("DepositWithdrawAction : Start");			
						
			DepositWithdrawActionForm formBean = (DepositWithdrawActionForm) form;
			if("init".equals(formBean.getAction())){
				formBean.resetExpect();
				formBean.setDepositWithdrawList(new ArrayList<DepositWithdrawBO>());
			} else if("search".equals(formBean.getAction())) search(formBean, request);	
			else if ("delete".equals(formBean.getAction())) delete(formBean, request);
			
			formBean.setAction("init");
			
			log.debug("DepositWithdrawAction : End");
			
		}catch(Exception e){
			log.error(e.getMessage());
			return (mapping.findForward("error"));
		}
		
		
		return mapping.findForward("depositWithdraw");
	}
	
	public void search(DepositWithdrawActionForm formBean, HttpServletRequest request) throws Exception {
		log.debug("DepositWithdrawAction : search : Start");	
	
		DepositWithdrawBO bo = new DepositWithdrawBO();
		bo.setCifNo(formBean.getCifNo());
		bo.setCustomerName(formBean.getCustomerName());
		bo.setMarginAccNo(formBean.getMarginAccNo());
		bo.setMarginAccName(formBean.getMarginAccName());
		bo.setTransactionDate(formBean.getTransactionDate());
		
		formBean.setDepositWithdrawList(service.searchDepositWithdraw(bo));
		request.setAttribute(Constant.SHOW_RESULT, Constant.SHOW_RESULT);
		
		log.debug("DepositWithdrawAction : search : End");
	}
	
	public void delete(DepositWithdrawActionForm formBean, HttpServletRequest request) throws Exception{
		log.debug("DepositWithdrawAction : delete : Start");
		
		if(formBean.getDeleteId() != null && !"".equals(formBean.getDeleteId())){
			service.delete(formBean.getUserLoginId(), formBean.getDeleteId());
			
			DepositWithdrawBO userBo = null;
			
			for(DepositWithdrawBO bo : formBean.getDepositWithdrawList()){
				if(formBean.getDeleteId().equals(bo.getId())){
					userBo = bo;
				}
			}
			
			formBean.getDepositWithdrawList().remove(userBo);
			
			int index = 1;
			
			for(DepositWithdrawBO bo : formBean.getDepositWithdrawList()){
				bo.setRowNum(index);
				index++; 
			}
		}
		
		request.setAttribute(Constant.SHOW_RESULT, Constant.SHOW_RESULT);
		
		log.debug("DepositWithdrawAction : delete : End");
	}
}
